import {PAGE_SIZE, CATEGORY_LIST} from '../constants/common';
import {load} from '../utils/Storage';
import {request} from '../utils/request';
import {action, flow, makeAutoObservable, observable} from 'mobx';
/**
 * home store 全局存储
 */
class HomeStore {
  homeList: ArticleSimple[] = [];
  page: number = 1;
  refreshing: boolean = false;
  finished: boolean = false; // 数据已经加载完了，没有更多数据
  edit: boolean = false; // 频道进入可编辑状态
  categoryList: Category[] = [];

  constructor() {
    makeAutoObservable(this, {
      homeList: observable,
      page: observable,
      refreshing: observable,
      finished: observable,
      categoryList: observable,
      resetPage: action,
      requestHomeList: action,
      removeCategoryFromMyList: action,
      addCategoryFromMyList: action,
      setEdit: action,
    });
  }

  resetPage = () => {
    this.page = 1;
  };

  setEdit = (value: boolean) => {
    this.edit = value;
  };
  removeCategoryFromMyList = (item: Category) => {
    this.categoryList = this.categoryList.filter(c => c.name !== item.name);
    this.categoryList.push({...item, isAdd: false});
  };

  addCategoryFromMyList = (item: Category) => {
    this.categoryList = this.categoryList.filter(c => c.name !== item.name);
    this.categoryList.push({...item, isAdd: true});
  };

  requestHomeList = flow(function* (
    this: HomeStore, // 方便使用this指针
  ) {
    if (this.refreshing || this.finished) {
      return;
    }
    try {
      this.refreshing = true;
      const params = {
        page: this.page,
        pageSize: PAGE_SIZE,
      };

      const {data} = yield request('homeList', params);
      if (data?.length) {
        if (this.page === 1) {
          this.homeList = data;
        } else {
          this.homeList = [...this.homeList, ...data];
        }
        this.page = this.page + 1;
      } else {
        if (this.page === 1) {
          this.homeList = [];
        } else {
          //已经加载完了
        }
        this.finished = true;
      }
    } catch (error) {
      console.log(error);
    } finally {
      this.refreshing = false;
    }
  });

  getCategoryList = flow(function* (
    this: HomeStore, // 方便使用this指针
  ) {
    const cacheListJSON = yield load(CATEGORY_LIST);
    if (cacheListJSON) {
      const cacheList = JSON.parse(cacheListJSON);
      if (cacheList?.length) {
        this.categoryList = cacheList;
      } else {
        this.categoryList = DEFAULT_CATEGORY_LIST;
      }
    } else {
      this.categoryList = DEFAULT_CATEGORY_LIST;
    }
  });
}

/**
 * ESM单例导出
 */
export default new HomeStore();

const DEFAULT_CATEGORY_LIST: Category[] = [
  // 默认添加频道
  {name: '推荐', default: true, isAdd: true},
  {name: '视频', default: true, isAdd: true},
  {name: '直播', default: true, isAdd: true},
  {name: '摄影', default: false, isAdd: true},

  {name: '穿搭', default: false, isAdd: true},
  {name: '读书', default: false, isAdd: true},
  {name: '影视', default: false, isAdd: true},
  {name: '科技', default: false, isAdd: true},

  {name: '健身', default: false, isAdd: true},
  {name: '科普', default: false, isAdd: true},
  {name: '美食', default: false, isAdd: true},
  {name: '情感', default: false, isAdd: true},

  {name: '舞蹈', default: false, isAdd: true},
  {name: '学习', default: false, isAdd: true},
  {name: '男士', default: false, isAdd: true},
  {name: '搞笑', default: false, isAdd: true},

  {name: '汽车', default: false, isAdd: true},
  {name: '职场', default: false, isAdd: true},
  {name: '运动', default: false, isAdd: true},
  {name: '旅行', default: false, isAdd: true},

  {name: '音乐', default: false, isAdd: true},
  {name: '护肤', default: false, isAdd: true},
  {name: '动漫', default: false, isAdd: true},
  {name: '游戏', default: false, isAdd: true},

  // 默认添加频道
  {name: '家装', default: false, isAdd: false},
  {name: '心理', default: false, isAdd: false},
  {name: '户外', default: false, isAdd: false},
  {name: '手工', default: false, isAdd: false},

  {name: '减脂', default: false, isAdd: false},
  {name: '校园', default: false, isAdd: false},
  {name: '社科', default: false, isAdd: false},
  {name: '露营', default: false, isAdd: false},

  {name: '文化', default: false, isAdd: false},
  {name: '机车', default: false, isAdd: false},
  {name: '艺术', default: false, isAdd: false},
  {name: '婚姻', default: false, isAdd: false},

  {name: '家居', default: false, isAdd: false},
  {name: '母婴', default: false, isAdd: false},
  {name: '绘画', default: false, isAdd: false},
  {name: '壁纸', default: false, isAdd: false},

  {name: '头像', default: false, isAdd: false},
];
